package com.swj.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swj.common.core.model.SysDictData;
import com.swj.common.core.model.web.RestResult;
import com.swj.common.core.utils.ResponseUtil;
import com.swj.common.core.utils.SysStringUtil;
import com.swj.common.log.annotation.Log;
import com.swj.common.log.enums.BusinessType;
import com.swj.common.security.annotation.RequiresPermissions;
import com.swj.modules.system.model.SysDictDataQueryVO;
import com.swj.modules.system.service.ISysDictDataService;
import com.swj.modules.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController {
    @Autowired
    private
    ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/page")
    public RestResult<Page<SysDictData>> page(SysDictDataQueryVO dictData) {
        Page<SysDictData> page = dictDataService.selectDictDataPage(dictData);
        return ResponseUtil.successWithPageList(page);
    }

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public RestResult<List<SysDictData>> list(SysDictDataQueryVO dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return ResponseUtil.successWithList(list);
    }

//    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:dict:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictData dictData) {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        util.exportExcel(response, list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public RestResult<SysDictData> getInfo(@PathVariable String dictCode) {
        return ResponseUtil.successWithObj(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public RestResult<List<SysDictData>> dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (SysStringUtil.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return ResponseUtil.successWithList(data);
    }

    /**
     * 新增字典类型
     *
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public RestResult<?> add(@Validated @RequestBody SysDictData dict) {
        if ((dictDataService.save(dict))) {
            return ResponseUtil.success("新增成功");
        } else {
            return ResponseUtil.fail("新增失败");
        }
    }

    /**
     * 修改保存字典类型
     *
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public RestResult<?> edit(@Validated @RequestBody SysDictData dict) {
        if((dictDataService.updateById(dict))){
            return ResponseUtil.success("修改成功");
        }else {
            return ResponseUtil.fail("修改失败");
        }

    }


//     * 删除字典类型
//
//    @RequiresPermissions("system:dict:remove")
//    @Log(title = "字典类型", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{dictCodes}")
//    public AjaxResult remove(@PathVariable Long[] dictCodes) {
//        dictDataService.deleteDictDataByIds(dictCodes);
//        return success();
//    }
}
